package com.insta.utils;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;

public class HeadsetConnectionReceiver extends BroadcastReceiver {

	private boolean headsetConnected = false;
	private MediaPlayer mediaPlayer = SingletonPlayer.getInstancePlayer();

	@Override
	public void onReceive(Context arg0, Intent intent) {
		if (intent.hasExtra("state")) {
			if (headsetConnected && intent.getIntExtra("state", 0) == 0) {
				headsetConnected = false;
				if (mediaPlayer.isPlaying()) {
					mediaPlayer.pause();
				}
			} else if (!headsetConnected && intent.getIntExtra("state", 0) == 1) {
				headsetConnected = true;
				if (!mediaPlayer.isPlaying()) {
					mediaPlayer.start();
				}
			}
		}
	}
}
